package 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;
	import mx.preloaders.IPreloaderDisplay;
	import mx.preloaders.Preloader;
	
	public class Loader extends Sprite implements IPreloaderDisplay
	{
		private var _backgroundAlpha:Number
		private var _backgroundColor:uint;
		private var _backgroundImage:Object;
		private var _backgroundSize:String;
		private var _preloader:Sprite;
		private var _stageHeight:Number;
		private var _stageWidth:Number;
		
		private var _progress:Shape;
		
		public function Loader()
		{
			_progress = new Shape();
			addChild(_progress);
		}
		
		public function initialize():void
		{
			_progress.x = stage.stageWidth / 2;
			_progress.y = stage.stageHeight / 2;
		}
		
		/* event handlers */
		
		private function onProgress(event:ProgressEvent):void
		{
			_progress.graphics.clear( );
			_progress.graphics.lineStyle(0, 0, 1);
			_progress.graphics.drawCircle(0, 0, 20);
			_progress.graphics.moveTo(0, 0);
			_progress.graphics.lineTo(0, 20);
			_progress.graphics.moveTo(0, 0);
			var angle:Number = event.bytesLoaded / event.bytesTotal * Math.PI * 2;
			var newX:Number = Math.sin(angle) * 20;
			var newY:Number = Math.cos(angle) * 20;
			_progress.graphics.lineTo(newX, newY);
		}
		
		private function onInitComplete(event:FlexEvent):void
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		/* getters / setters */

		public function get backgroundAlpha():Number
		{
			return _backgroundAlpha;
		}

		public function set backgroundAlpha(value:Number):void
		{
			_backgroundAlpha = value;
		}

		public function get backgroundColor():uint
		{
			return _backgroundColor;
		}

		public function set backgroundColor(value:uint):void
		{
			_backgroundColor = value;
		}

		public function get backgroundImage():Object
		{
			return _backgroundImage;
		}

		public function set backgroundImage(value:Object):void
		{
			_backgroundImage = value;
		}

		public function get backgroundSize():String
		{
			return _backgroundSize;
		}

		public function set backgroundSize(value:String):void
		{
			_backgroundSize = value;
		}

		public function get preloader():Sprite
		{
			return _preloader;
		}

		public function set preloader(value:Sprite):void
		{
			_preloader = value as Preloader;
			value.addEventListener(ProgressEvent.PROGRESS, onProgress);
			value.addEventListener(FlexEvent.INIT_COMPLETE, onInitComplete);
		}

		public function get stageHeight():Number
		{
			return _stageHeight;
		}

		public function set stageHeight(value:Number):void
		{
			_progress.y = value / 2;
			_stageHeight = value;
		}

		public function get stageWidth():Number
		{
			return _stageWidth;
		}

		public function set stageWidth(value:Number):void
		{
			_progress.x = value / 2;
			_stageWidth = value;
		}
	}
}
